<template>
	<div class="system-role-dialog-container energy-from energy-dialog">
		<el-dialog center align-center v-model="state.dialog.isShowDialog" width="769px" @close="resetData">
			<template #header="{ titleClass }">
				<div :class="titleClass">{{ state.dialog.isAdd ? '新增角色' : '编辑角色' }}</div>
			</template>
			<el-form ref="roleDialogFormRef" :model="state.ruleForm" :rules="state.rules" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色名称" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入角色名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色标识" prop="roleKey">
							<template #label>
								<el-tooltip effect="dark" content="用于 `router/route.ts` meta.roles" placement="top-start">
									<span>角色标识</span>
								</el-tooltip>
							</template>
							<el-input v-model="state.ruleForm.roleKey" placeholder="请输入角色标识" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="角色类型">
							<el-radio-group v-model="state.ruleForm.type">
								<el-radio :label="0">系统类型(不可删除)</el-radio>
								<el-radio :label="1">用户类型(可删除)</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色状态">
							<el-switch
								v-model="state.ruleForm.status"
								size="default"
								:active-value="0"
								:inactive-value="1"
								active-text="启用"
								inactive-text="禁用"
								inline-prompt
								style="--el-switch-on-color: #13ce66"
							/>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注内容">
							<el-input v-model="state.ruleForm.remark" placeholder="请输入备注内容" show-word-limit type="text" maxlength="20"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<div class="flex row-center mt10 mb10">
				<div class="btn3" @click="onCancel">
					<el-icon> <ele-Close /> </el-icon><span class="ml6">取 消</span>
				</div>
				<div class="btn1 ml10" @click="onSubmit">
					<el-icon> <ele-Check /> </el-icon><span class="ml6">保 存</span>
				</div>
			</div>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemRoleDialog">
import { reactive, ref, nextTick } from 'vue';
import { useRoleApi } from '/@/api/role/index';
import { ElNotification } from 'element-plus';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
// 定义变量内容
const roleDialogFormRef = ref();
const state: any = reactive({
	ruleForm: {
		id: '',
		name: '', // 角色名称
		roleKey: '', // 角色标识
		status: 0, // 角色状态
		remark: '', // 角色描述
		type: 0, // 0系统类型(不可删除) 1用户类型(可删除)
		menuIds: [],
		// menuIds: [
		// 	{
		// 		menuId: '', // 菜单id
		// 		roleId: '', // 角色id
		// 	},
		// ], // 权限关联关系集合
	},
	dialog: {
		isShowDialog: false,
		isAdd: true,
	},
	rules: {
		name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
		roleKey: [{ required: true, message: '角色标识不能为空', trigger: 'blur' }],
		// menuIds: [{ required: true, message: '请选择角色权限', trigger: 'blur' }],
	},
});

// 打开弹窗
const openDialog = (row: any) => {
	state.dialog.isShowDialog = true;
	if (row) {
		state.ruleForm = {
			id: row.id,
			name: row.name, // 角色名称
			roleKey: row.roleKey, // 角色标识
			status: row.status, // 角色状态
			remark: row.remark, // 角色描述
			type: row.type, // 角色类型
		};
		nextTick(() => {
			getCheckedRoles(row.id);
		});

		state.dialog.isAdd = false;
	} else {
		state.dialog.isAdd = true;
	}
};

// 根据id获取角色详情-已选中的角色(selected  0否 1是)
const getCheckedRoles = async (id: string) => {
	const { data } = await useRoleApi().getRoleDetailById(id);
	let tileData: any = [];
	function changMenuData(data: []) {
		data.forEach((item: any) => {
			tileData.push(item);
			if (item.childList && item.childList.length > 0) {
				changMenuData(item.childList);
			}
		});
	}
	changMenuData(data.menus);
	state.ruleForm.menuIds = [];
	tileData.map((item: any) => {
		if (item.selected !== 0) {
			state.ruleForm.menuIds.push(item.id);
		}
	});
};

// 新增角色
const handleAddRole = async () => {
	const data = await useRoleApi().addRole(state.ruleForm);
	if (data.code == 200) {
		emit('refresh');
		ElNotification({
			title: '提示',
			message: '新增角色成功',
			position: 'top-right',
			type: 'success',
		});
	}
};

// 编辑角色
const handleEditRole = async () => {
	const data = await useRoleApi().updateRole(state.ruleForm);
	if (data.code == 200) {
		emit('refresh');
		ElNotification({
			title: '提示',
			message: '修改角色成功',
			position: 'top-right',
			type: 'success',
		});
	}
};

// 提交
const onSubmit = () => {
	roleDialogFormRef.value.validate((valid: any) => {
		if (valid) {
			state.dialog.isAdd ? handleAddRole() : handleEditRole();
		} else {
			ElNotification({
				title: '提示',
				message: '校验失败,请填写完整角色信息',
				position: 'top-right',
				type: 'error',
			});
		}
		closeDialog();
	});
};

// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
	closeDialog();
};

// 表单清空
const resetData = () => {
	nextTick(() => {
		roleDialogFormRef.value.resetFields();
		state.ruleForm = {
			id: '',
			name: '', // 角色名称
			roleKey: '', // 角色标识
			status: 0, // 角色状态
			remark: '', // 角色描述
			type: 0, // 角色类型
			menuIds: [],
		};
	});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
